{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation and deployment of 5G Core (5GC) of Open Air Interface (OAI)\n",
    "## Objectives\n",
    "- Install and configure the 5GC of OAI in a virtual machine Ubuntu 20.04 LTS (Focal Fossa)\n",
    "- Deploy 5GC Core\n",
    "- Inspec 5G Core Docker containers by means of command line instructions \n",
    "- Install and configure Portainer tool\n",
    "- Inspec 5G Core Docker containers by means of Portainer browser"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At the end of a set of three hands-on (this is the first one), the goal is to run the simulated OAI scenario, which consists of the 5G core (5GC), gNB in CU/DU split mode, and user equipment (UE), according to the image below: \n",
    "\n",
    "![5GC_01](../FIGS/H01_5GCore_UNI_III/5GC_deploy.jpg)\n",
    "\n",
    "Now, we are going to install, configure, deploy and interact to 5GC.\n",
    "\n",
    "\n",
    "## General Instructions\n",
    "\n",
    "In this task you will install and configure the core in its basic architecture according to the image below:\n",
    "\n",
    "![5GC_01](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_01.png)\n",
    "\n",
    "**Minimum hardware requirements for OAI UE, according to doc/NR_SA_Tutorial_COTS_UE.md · develop · oai / openairinterface5G · GitLab (eurecom.fr):**\n",
    "- Ubuntu 20.04.5 LTS;\n",
    "- CPU: 4 cores x86_64;\n",
    "- 8 GB RAM.\n",
    "\n",
    "**Minimum hardware requirements for OAI 5GC and OAI gNB, according to doc/NR_SA_Tutorial_COTS_UE.md · develop · oai / openairinterface5G · GitLab (eurecom.fr):**\n",
    "- Ubuntu 20.04.5 LTS;\n",
    "- CPU: 8 cores x86_64 3.5 GHz;\n",
    "- 32 GB RAM.\n",
    "\n",
    "**However, if you do not have such hardware specifications, you may use the one below for both devices (tested for us, you may face some lags, but it works):**\n",
    "- Ubuntu 20.04.5 LTS;\n",
    "- CPU: 4 cores x86_64;\n",
    "- 4 GB RAM."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5GC Installation\n",
    "\n",
    "In a Linux Terminal, follow the steps below:\n",
    "\n",
    "**Step 0:** Setup your VM or Baremetal Machine, after install Useful Software:\n",
    "\n",
    "sudo su\n",
    "\n",
    "apt update -y\n",
    "\n",
    "apt upgrade -y\n",
    "\n",
    "apt install -y python3-pip git curl cmake tree golang vim net-tools build-essential nfs-common openssh-server\n",
    "\n",
    "**Step 1:** Install dependencies\n",
    "\n",
    "sudo apt-get install ca-certificates \n",
    "\n",
    "sudo apt-get install curl \n",
    "\n",
    "sudo apt-get install gnupg \n",
    "\n",
    "sudo apt-get install lsb-release \n",
    "\n",
    "**Step 2:** Enable packet forwarding\n",
    "\n",
    "sudo sysctl net.ipv4.conf.all.forwarding=1\n",
    "\n",
    "sudo iptables -P FORWARD ACCEPT\n",
    "\n",
    "**Step 3:** Install docker and docker-compose\n",
    "\n",
    "<!--sudo rm /etc/apt/sources.list.d/docker.list*\n",
    "\n",
    "sudo mkdir -p /etc/apt/keyrings \n",
    "\n",
    "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -\n",
    "\n",
    "sudo add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/ubuntu \\\\$(. /etc/os-release; echo \"\\\\$UBUNTU_CODENAME\") stable\"\n",
    "\n",
    "sudo chmod a+r /etc/apt/keyrings/docker.gpg\n",
    "\n",
    "sudo apt-get update\n",
    "\n",
    "sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin\n",
    "\n",
    "```python \n",
    "sudo curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose\n",
    "```\n",
    "\n",
    "sudo chmod +x /usr/local/bin/docker-compose\n",
    "-->\n",
    "\n",
    "sudo apt install -y ca-certificates curl gnupg\n",
    "\n",
    "sudo install -m 0755 -d /etc/apt/keyrings\n",
    "\n",
    "sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg\n",
    "\n",
    "sudo chmod a+r /etc/apt/keyrings/docker.gpg\n",
    "\n",
    "```bash\n",
    "sudo echo \"deb [arch=\"$(dpkg --print-architecture)\" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \"$(. /etc/os-release && echo \"$VERSION_CODENAME\")\" stable\" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null\n",
    "```\n",
    "\n",
    "sudo apt update\n",
    "\n",
    "sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin\n",
    "\n",
    "```bash\n",
    "curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose\n",
    "```\n",
    "\n",
    "chmod +x /usr/local/bin/docker-compose\n",
    "\n",
    "**Step 4:** Pull the images from Docker Hub\n",
    "\n",
    "sudo docker pull oaisoftwarealliance/oai-amf:v1.5.0\n",
    "\n",
    "sudo docker pull oaisoftwarealliance/oai-nrf:v1.5.0\n",
    "\n",
    "sudo docker pull oaisoftwarealliance/oai-smf:v1.5.0\n",
    "\n",
    "sudo docker pull oaisoftwarealliance/oai-udr:v1.5.0\n",
    "\n",
    "sudo docker pull oaisoftwarealliance/oai-udm:v1.5.0\n",
    "\n",
    "sudo docker pull oaisoftwarealliance/oai-ausf:v1.5.0\n",
    "\n",
    "sudo docker pull oaisoftwarealliance/oai-spgwu-tiny:v1.5.0\n",
    "\n",
    "sudo docker pull oaisoftwarealliance/trf-gen-cn5g:latest\n",
    "\n",
    "**Step 5:** Tag Docker Images\n",
    "\n",
    "sudo docker image tag oaisoftwarealliance/trf-gen-cn5g:latest trf-gen-cn5g:latest\n",
    "\n",
    "sudo docker image tag oaisoftwarealliance/oai-amf:v1.5.0 oai-amf:v1.5.0\n",
    "\n",
    "sudo docker image tag oaisoftwarealliance/oai-nrf:v1.5.0 oai-nrf:v1.5.0\n",
    "\n",
    "sudo docker image tag oaisoftwarealliance/oai-smf:v1.5.0 oai-smf:v1.5.0\n",
    "\n",
    "sudo docker image tag oaisoftwarealliance/oai-udr:v1.5.0 oai-udr:v1.5.0\n",
    "\n",
    "sudo docker image tag oaisoftwarealliance/oai-udm:v1.5.0 oai-udm:v1.5.0\n",
    "\n",
    "sudo docker image tag oaisoftwarealliance/oai-ausf:v1.5.0 oai-ausf:v1.5.0\n",
    "\n",
    "sudo docker image tag oaisoftwarealliance/oai-spgwu-tiny:v1.5.0 oai-spgwu-tiny:v1.5.0\n",
    "\n",
    "**Step 6:** Clone OpenAirInterface 5G Core repository\n",
    "\n",
    "git clone https://gitlab.eurecom.fr/oai/cn5g/oai-cn5g-fed.git\n",
    "\n",
    "cd oai-cn5g-fed\n",
    "\n",
    "git checkout -f v1.5.0\n",
    "\n",
    "cd ..\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "## 5GC Configuration\n",
    "\n",
    "In a Linux Terminal, follow the steps below:\n",
    "\n",
    "**Step 1:** Configuration\n",
    "\n",
    "The configuration will be performed using docker-compose. First, run the command below to download and copy the OAI core docker-compose file. We consider that <OAI_PATH> (path that you install OAI) is **/home/dco1020** (the file will already be placed in the correct path <OAI_PATH>/oai-cn5g-fed/docker-compose/). Please, change <OAI_PATH> by **/home/dco1020** in the following commands. In case you install in a different folder, change commands properly.\n",
    "\n",
    "git clone https://github.com/vicentesousa/DCO1020.git\n",
    "\n",
    "cp ./DCO1020/CODES/H01_5GCore_UNI_III/docker-compose-basic-nrf.yaml  /home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml\n",
    "\n",
    "Then copy the database used for this deployment. This database contains important information such as data for gNB and UE authentication. The command below will download and place the file already in the correct path (PATH_TO/oai-cn5g-fed/docker-compose/database/).\n",
    "\n",
    "cp /home/dco1020/DCO1020/CODES/H01_5GCore_UNI_III/oai_db.sql /home/dco1020/oai-cn5g-fed/docker-compose/database/oai_db.sql \n",
    "\n",
    "**Step 2:** Synchronizing the tutorials\n",
    "\n",
    "cd /home/dco1020/oai-cn5g-fed/\n",
    "\n",
    "sudo ./scripts/syncComponents.sh\n",
    "\n",
    "sudo git submodule deinit --all --force\n",
    "\n",
    "sudo git submodule init\n",
    "\n",
    "sudo git submodule update\n",
    "\n",
    "**Step 3:** Create the bridge 5GC\n",
    "\n",
    "The command below will then create a subnet that will provide communication between the containers in the core.\n",
    "\n",
    "sudo docker network create --driver=bridge --subnet=192.168.70.128/26 -o \"com.docker.network.bridge.name\"=\"demo-oai\" demo-oai-public-net"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deployment\n",
    "\n",
    "For this task, open another terminal using crtl+Alt+T.\n",
    "\n",
    "**Step 1:** Core deployment\n",
    "\n",
    "cd /home/dco1020/oai-cn5g-fed/docker-compose\n",
    "\n",
    "sudo python3 core-network.py --type start-basic --scenario 1\n",
    "\n",
    "Expected result:\n",
    "\n",
    "After running the above command, nine containers are expected to be in the \"Up (healthy)\" state as shown in figure below.\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_02.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Verificação dos Pods com linha de comando e com o Portainer\n",
    "\n",
    "The 5GC components were installed as docker containers. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. Container images become containers at runtime and in the case of Docker containers – images become containers when they run on Docker Engine. Available for both Linux and Windows-based applications, containerized software will always run the same, regardless of the infrastructure. Containers isolate software from its environment and ensure that it works uniformly despite differences for instance between development and staging.\n",
    "\n",
    "Containers and virtual machines have similar resource isolation and allocation benefits, but function differently because containers virtualize the operating system instead of hardware. Containers are more portable and efficient. Figure bellow illustrates the difference between VMs and containers.\n",
    "\n",
    "![5GC_deploy_04](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_04.png)\n",
    "\n",
    "**Step 1**: to see all 5GC docker containers by command line you could type the following command in a terminal:\n",
    "\n",
    "docker ps -a\n",
    "\n",
    "The results are as bellow:\n",
    "\n",
    "![5GC_deploy_03](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_03.png)\n",
    "\n",
    "You can see all virtual functions that comprise the 5GC.\n",
    "\n",
    "\n",
    "**Step 2**: let's logs of a running docker container, for instance, let's see logs of the 5GC's AMF container. In a terminal , type:\n",
    "\n",
    "docker logs -f 2de7bfa9afa4\n",
    "\n",
    "You will see a screen similar to the figure bellow.\n",
    "\n",
    "![5GC_deploy_05](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_05.png)\n",
    "\n",
    "\n",
    "You can find the same results with the command:\n",
    "\n",
    "docker logs -f oai-amf\n",
    "\n",
    "**Step 3**: you can also see the hardware resources all containers are consuming. In a terminal , type:\n",
    "\n",
    "docker container stats\n",
    "\n",
    "You will see a screen similar to the figure bellow.\n",
    "\n",
    "![5GC_deploy_06](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_06.png)\n",
    "\n",
    "To return to terminal, type: crtl+c\n",
    "\n",
    "**Step 4**: let's execute a command inside a running docker container, for instance, let's open a bash inside the 5GC's AMF container and see the processes in execution. See in figures above that **2de7bfa9afa4** is the container ID of the 5GC's AMF container. So, in a terminal , type (ps.: realize that you need to see the containerID of 5GC's AMF installed in your machine):\n",
    "\n",
    "docker exec -it 2de7bfa9afa4 bash\n",
    "\n",
    "You will see a screen similiar to the figure bellow.\n",
    "\n",
    "![5GC_deploy_07](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_07.png)\n",
    "\n",
    "As you have a bash shell inside the container, you can type bash commands. Let's see all processes running by this container. Type:\n",
    "\n",
    "top\n",
    "\n",
    "You will see a screen similiar to the figure bellow.\n",
    "\n",
    "![5GC_deploy_08](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_08.png)\n",
    "\n",
    "To return to terminal, type: crtl+c\n",
    "\n",
    "**Step 5 (optional)**: To stop the core network you can use:\n",
    "\n",
    "python3 core-network.py --type stop-basic --scenario 1\n",
    "\n",
    "\n",
    "A good Docker Cheat Sheet can be found [here](https://docs.docker.com/get-started/docker_cheatsheet.pdf).\n",
    "\n",
    "\n",
    "**Step 6 (optional)**: there is a plenty of graphical tool to manager, see and operate dockers. Let's install and use Portainer. To install it, first, create the volume that Portainer Server will use to store its database. Ia a terminal, type:\n",
    "\n",
    "docker volume create portainer_data\n",
    "\n",
    "**Step 7 (optional)**: Then, download and install the Portainer Server container:\n",
    "\n",
    "docker volume create portainer_data\n",
    "\n",
    "docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest\n",
    "\n",
    "More details [in this link.](https://docs.portainer.io/start/install-ce/server/docker/linux)\n",
    "\n",
    "**Step 8 (optional)**: Now that the installation is complete, you can log into your Portainer Server instance by opening a web browser and going to:\n",
    "\n",
    "https://localhost:9443\n",
    "\n",
    "\n",
    "**Step 9 (optional)**: You need to create a user. For this, you will see a screen similar to:\n",
    "\n",
    "![5GC_port_8](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_08.png)\n",
    "\n",
    "**Step 10 (optional)**: Now, you can see the initial screen and you could proceedd using the local environment. Please, click on **Get Started**.\n",
    "\n",
    "![5GC_port_9](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_09.png)\n",
    "\n",
    "**Step 11 (optional)**: then, you can see the screen with the local environment. Click on it.\n",
    "\n",
    "![5GC_port_10](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_10.png)\n",
    "\n",
    "**Step 12 (optional)**: Now, you could see the environment dashboard with all information about your Portainer. Let's see all Containers clicking on Containers menu as indicating below.\n",
    "\n",
    "![5GC_port_11](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_11.png)\n",
    "\n",
    "**Step 13 (optional)**: you can see all containers, their status and a menu of quick actions. \n",
    "\n",
    "![5GC_port_12](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_12.png)\n",
    "\n",
    "**Step 14 (optional)**: To see the AMF container logs, please click on icone indicated in the figure below.\n",
    "\n",
    "![5GC_port_13](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_13.png)\n",
    "\n",
    "**Step 15 (optional)**: Finally, you can see AMF logs (note that you can filter and download it).\n",
    "\n",
    "![5GC_port_14](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_14.png)\n",
    "\n",
    "An interesting video about Portainer tool is: [Controle seus containers de maneira SIMPLES com o Portainer - Diolinux Labs](https://www.youtube.com/watch?v=q0LrLN4fylw),\n",
    "\n",
    "# Deliverable 01:\n",
    "See the log of all docker containers of 5GC and identify the following information:\n",
    "\n",
    "- GUAMI (MCC, MNC, Region ID, AMF Set ID, AMF pointer):\n",
    "- Slice Support: what are SST, SD supported? Explain those parameters. Indicate the 5QI of each slice.\n",
    "- What is the N2 IP Address and port?\n",
    "- What is the NRF IP Address and port?\n",
    "- What is the AUSF IP Address and port?\n",
    "- What is the AMF IP Address and port?\n",
    "- What is the N4 IP Address and port?\n",
    "- Em qual entidade é configurada a faixa de IPs do UEs que se conecta a rede 5G? Mostre esse configuração e qual a faixa de IPs.\n",
    "\n",
    "Show the logs that you see all information."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
